﻿using UnityEngine;
using System.Collections;

public class ChangeScene : MonoBehaviour 
{
    public string scene;
    private bool isOver = false;
    public Texture2D[] textures = new Texture2D[2];

	void Update () 
    {
        Touch[] touch = Input.touches;

        if(Input.touchCount > 0)
        {
            Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch[0].position.x, touch[0].position.y, 0));
            RaycastHit hit;

            if(Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                if(hit.collider.gameObject == gameObject)
                {
                    if(touch[0].phase == TouchPhase.Began)
                    {
                        this.isOver = true;
                    }

                    if(touch[0].phase == TouchPhase.Ended)
                    {
                        if(gameObject.tag == "Exit")
                        {
                            Application.Quit();
                        }
                        else
                        {
                            Application.LoadLevel(scene);
                        }
                    }
                }
                else
                {
                    this.isOver = false;
                }
            }
        }

        if(textures[0] != null)
        {
            if(isOver)
            {
                renderer.material.mainTexture = textures[1];
            }
            else
            {
                renderer.material.mainTexture = textures[0];
            }
        }
	}
}